App({
   globalData: {
      shopInfo: '',
      tableCateArr: '',
      loginMode: 'guest',
      guestInfo: '',
      wssIsInit: 0,
      isWsSender: 0,//websocket发送者，1是，0否
      client_id: '',
      rid: ''//授权登录room_id
   },

   readyWebsocket: function (callback) {
      if (callback) {
         this.wssCallback = callback
      }
      console.log('【this】', this)
      var tg = this.globalData
      if (tg.wssIsInit) {
         if (tg.client_id == '') {
            this.loginWebsocket()
         } else {
            this.logoutWss()
         }
      } else {
         this.initWebsocket()
      }
   },

   initWebsocket: function () {
      var that = this
      wx.connectSocket({
         url: 'wss://wss.openally.com',
         // url: 'wss://wss.suiyuantech.com',
         success: function (res) {
            console.log('wx.connectSocket连接成功')
            wx.onSocketOpen(function (res) {
               that.onSocketMsg()
               console.log('wx.onSocketOpen打开成功')
               that.loginWebsocket('login')
            })
         },
         fail: function (res) {
            console.log('wx.connectSocket连接失败')
         },
      })

      wx.onSocketClose(function (res) {
         console.log('#############wx.onSocketClose关闭了', res)
         that.loginWebsocket()
      })
   },

   onSocketMsg: function () {
      var t = this
      var tg = t.globalData
      wx.onSocketMessage(function (res) {
         if (!tg.wssIsInit) tg.wssIsInit = 1
         var data = JSON.parse(res.data)
         console.log('【app.js收到wss信息data】', data, '\n【全局变量】', t.globalData)
         if (data.type == 'login' && tg.guestInfo.name == data.client_name) {
            if (tg.client_id == '') {
               tg.client_id = data.client_id
            }
            if (tg.rid != '') {//如果是授权，也返回
               if (t.wssCallback) {
                  t.wssCallback(data)
               }
            }
         }
         if (data.notice) {
            var N = JSON.parse(data.notice)
            if (t.wssCallback) {
               t.wssCallback(N)
            }
         }
      })
   },

   loginWebsocket: function () {
      var t_g = this.globalData
      var json = {
         type: 'login',
         // name: t_g.guestInfo.user,
         client_name: t_g.guestInfo.name,
         client_logo: t_g.guestInfo.logo,
         room_id: t_g.rid == '' ? t_g.shopInfo.Biz_ID : t_g.rid
      }
      if (t_g.rid != '') {
         json.auth = {
            unionid: t_g.guestInfo.unionid,
            logo: t_g.guestInfo.logo,
            userid: t_g.guestInfo.userid
         }
      }
      console.log('loginWebsocket: json', json)
      var msg = JSON.stringify(json)
      this.sendSocketMsg(msg)
      console.log('loginWebsocket:全局变量t_g', t_g)
   },

   logoutWss: function () {
      var tg = this.globalData
      var json = {
         type: 'logout',
         // name: tg.guestInfo.user,
         client_name: tg.guestInfo.name,
         client_id: tg.client_id,
         room_id: tg.rid == '' ? tg.shopInfo.shopInfo.userid : tg.rid
      }
      var msg = JSON.stringify(json)
      this.sendSocketMsg(msg)
      tg.client_id = ''
      tg.rid = ''
      console.log('退出wss：this.globalData', this.globalData)
   },

   sendSocketMsg: function (msg) {
      wx.sendSocketMessage({
         data: msg,
         success: function (res) {
            console.log('sendSocketMsg:发送成功')
         },
         fail: function (res) {
            console.log('sendSocketMsg:发送失败')
         },
      })
   },

   playMp3: function (state) {//有新消息声音提醒
      var audio = state == 2 ? 'new_order.wav' : 'new_msg.mp3'//'notify.ogg'
      wx.playBackgroundAudio({
         dataUrl: 'http://img.openally.com/images/' + audio,
         success: function (res) {
            console.log('play success: ', res)
         },
         fail: function (res) {
            console.log('play fail: ', res)
         },
      })
   },
   //商品分类变更通知(后台)
   setNoticeGoodsCatePage: function (notice) {
      this.notice = notice
   },
   noticeUpdateGoodsCate: function (data) {
      if (this.notice) {
         this.notice(data)
      }
   },
   //某餐桌详情变更通知(前台)
   setNoticeTableList: function (notice) {
      this.tableListNotice = notice
   },
   noticeUpdateTableList: function (data) {
      if (this.tableListNotice) {
         this.tableListNotice(data)
      }
   },
   // //某餐桌详情变更通知(后台)
   // setNoticeTableListAdmin: function (notice) {
   //    this.tableListAdminNotice = notice
   // },
   // noticeUpdateTableListAdmin: function (data) {
   //    if (this.tableListAdminNotice)
   //       this.tableListAdminNotice(data)

   //    if (this.tableListNotice)
   //       this.tableListNotice('reload')
   // },

   // //服务员增改通知(后台)
   // setNoticeWaiterList: function (noti) {
   //    this.waiterNotice = noti
   // },
   // noticeUpdateWaiterList: function (data) {
   //    if (this.waiterNotice)
   //       this.waiterNotice(data)
   // },
   //顾客点餐后通知生成小票
   setNoticeTicket: function (noti) {
      this.ticketNotice = noti
   },
   noticeTicket: function (noti) {
      if (this.ticketNotice)
         this.ticketNotice(noti)
   },

   //編辑商品后的通知更新
   setNoticetGoodsList: function (noti) {
      this.goodsListNotice = noti
   },
   noticeGoodsList: function (noti) {
      if (this.goodsListNotice)
         this.goodsListNotice(noti)
   },

   getJsonData: function (urlTail, param, callback) {
      // var aipUrl = "https://qapi.openally.com/api_qzg/" + urlTail
      var aipUrl = "https://api.igoyp.com/" + urlTail
      this.request(aipUrl, param, callback)
   },

   getNetData: function (urlTail, param, callback) {
      var aipUrl = "https://qapi.openally.com/api/public/" + urlTail
      // var aipUrl = "https://api.igoyp.com/" + urlTail
      this.request(aipUrl, param, callback)
   },

   request: function (aipUrl, param, callback) {
      var that = this
      wx.request({
         url: aipUrl,
         data: param,
         method: 'POST',
         success: function (res) {
            console.log('【地址】 ' + aipUrl, '\n【参数】', param, '\n【成功】', res);
            var d = res.data
            // if (d.code == 0 || d.code == 92900 || d.code == 81) {
            //    callback(res.data)
            // } else that.toast(d.message)
            if (d.code > 29300 && d.code < 30000) {
               that.toast(d.msg)
            } else if (typeof (callback) == 'function') {
               callback(d)
            }
         },
         fail: function (res) {
            console.log('【地址】 ' + aipUrl, '\n【参数】', param, '\n【失败】', res);
         },
      })
   },

   toast: function (msg) {
      wx.showToast({
         title: msg,
         image: '/pages/images/qzg_logo_90x.jpg',
      })
   },

   modal: function (msg, title) {
      wx.showModal({
         title: title ? title : '温馨提示',
         content: msg,
      })
   },

   confirm: function (title, text, cancelText, confirmText, callback) {
      wx.showModal({
         title: title,
         content: text,
         showCancel: true,
         cancelText: '',
         cancelColor: '',
         cancelText: cancelText,
         confirmText: confirmText,
         confirmColor: '',
         success: function (res) {
            console.log('confirm:success:', res)
            if (callback) callback(res.confirm)
         },
         fail: function (res) { },
      })
   },
   empty: function (val) {
      if (val == undefined || val == null || val == '' || val == 'undefined') {
         return true;
      } else {
         return false;
      }
   }
})
